// --- CONFIG: set receiver & auth here (server-side only) --- private $receiver_api_base = 'https://publictv.in/app/wp-json/wp/v2/'; private $receiver_media_base = 'https://publictv.in/app/wp-json/publictv/v1/'; private $auth_user = 'Bunny'; private $auth_pass = '3uVV kVBV rFlV 36aY 7cNl oBxv'; // --- helper to build auth header --- private function auth_header() { return 'Basic ' . base64_encode( $this->auth_user . ':' . $this->auth_pass ); } // --- Do NOT sync auto-drafts / quick autosaves --- function should_sync_post($post) { // skip autosave & revisions & auto-draft if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return false; if (wp_is_post_revision($post->ID)) return false; if ($post->post_status === 'auto-draft') return false; return true; } // --- Find remote ID stored in post meta --- private function get_local_remote_id($post_id) { $v = get_post_meta($post_id, '_publictv_remote_id', true); return $v ? intval($v) : false; } // --- Save remote id mapping on source post --- private function set_local_remote_id($post_id, $remote_id) { update_post_meta($post_id, '_publictv_remote_id', intval($remote_id)); } // --- Upload image to /app using base64 JSON endpoint --- private function upload_image_to_receiver($image_url) { if (empty($image_url)) return false; $image_data = @file_get_contents($image_url); if ($image_data === false) return false; $basename = basename(parse_url($image_url, PHP_URL_PATH)); if (empty($basename)) $basename = 'image-' . time() . '.jpg'; $body = [ 'filename' => $basename, 'file_base64' => base64_encode($image_data) ]; $res = wp_remote_post($this->receiver_media_base . 'media_upload', [ 'headers' => [ 'Authorization' => $this->auth_header(), 'Content-Type' => 'application/json', ], 'body' => json_encode($body), 'timeout' => 120 ]); if (is_wp_error($res)) return false; $code = wp_remote_retrieve_response_code($res); if ($code < 200 || $code >= 300) return false; $body = json_decode(wp_remote_retrieve_body($res), true); return isset($body['id']) ? intval($body['id']) : false; } // --- Find remote post id by meta mapping, fallback to slug search --- private function find_remote_post_id($post_id, $slug) { $mapped = $this->get_local_remote_id($post_id); if ($mapped) return $mapped; // fallback: search by slug $res = wp_remote_get($this->receiver_api_base . 'posts?slug=' . urlencode($slug), [ 'headers' => ['Authorization' => $this->auth_header()], 'timeout' => 30 ]); if (is_wp_error($res)) return false; $body = json_decode(wp_remote_retrieve_body($res), true); if (!empty($body) && isset($body[0]['id'])) { return intval($body[0]['id']); } return false; } // --- Main sync function to attach to save_post (replace your previous sync_post) --- function sync_post($post_id, $post, $update) { if (! $this->should_sync_post($post) ) return; // ensure only selected post types (post + story types) if (! in_array($post->post_type, $this->story_types) ) return; $remote_id = $this->find_remote_post_id($post_id, $post->post_name); $data = [ 'title' => $post->post_title, 'content' => $post->post_content, 'excerpt' => $post->post_excerpt, 'slug' => $post->post_name, 'status' => $post->post_status, 'date' => $post->post_date, 'meta' => get_post_meta($post_id), ]; // Featured image -> upload to receiver and attach $thumb = get_post_thumbnail_id($post_id); if ($thumb) { $img_url = wp_get_attachment_url($thumb); $remote_img_id = $this->upload_image_to_receiver($img_url); if ($remote_img_id) $data['featured_media'] = $remote_img_id; } // Attach story pages if non-post (web story types) if ($post->post_type !== 'post') { $attachments = get_attached_media('', $post_id); $remote_pages = []; foreach ($attachments as $media) { $img_url = wp_get_attachment_url($media->ID); $remote_img_id = $this->upload_image_to_receiver($img_url); if ($remote_img_id) $remote_pages[] = $remote_img_id; } if (!empty($remote_pages)) $data['story_media'] = $remote_pages; } // Decide endpoint and method if ($remote_id) { $endpoint = 'posts/' . $remote_id; $method = 'POST'; // WP REST accepts POST to update } else { $endpoint = 'posts'; $method = 'POST'; } $res = wp_remote_post($this->receiver_api_base . $endpoint, [ 'headers' => [ 'Authorization' => $this->auth_header(), 'Content-Type' => 'application/json' ], 'body' => json_encode($data), 'timeout' => 60 ]); if (is_wp_error($res)) { return; } $code = wp_remote_retrieve_response_code($res); if ($code >= 200 && $code < 300) { $body = json_decode(wp_remote_retrieve_body($res), true); if (!empty($body['id'])) { // save mapping so future updates use direct ID -> prevents duplicates $this->set_local_remote_id($post_id, intval($body['id'])); } } }